/*
 * @lc app=leetcode.cn id=101 lang=typescript
 *
 * [101] 对称二叉树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

//  思路：
//  将树分为左右子树，判断左右子树各镜像节点是否相等。

//  复杂度：O(n) O(n)

function isSymmetric(root: TreeNode | null): boolean {
    if (!root) return true
    const same = (root1: TreeNode | null, root2: TreeNode | null): boolean => {
        if (!root1 && !root2) return true
        if (root1 && root2 && root1.val === root2.val) {
            return same(root1.left, root2.right) && same(root1.right, root2.left)
        } else {
            return false
        }

    }
    return same(root.left, root.right)
};
// @lc code=end
import { TreeNode } from './type'

const root = new TreeNode(1, new TreeNode(2, new TreeNode(3), new TreeNode(4)), new TreeNode(2, new TreeNode(4), new TreeNode(3)))

console.log(isSymmetric(root));
